<?php

/**
 *  I_Controller.
 * 
 *  Esse controller extende o controller do Framework Codeigniter
 *  
 *  @package Core
 *  @subpackage Interface
 *  @category Controllers
 *  @author Carlos O Carvalho
 *  @version 1.0.0
 *  @access private
 *  */
class I_Controller extends CI_Controller {

    /**
     *
     * @var array $js 
     * contaneier javascripts files used in render template view 
     */
    private $js = array(
        'header' => array(),
        'footer' => array(),
        'ie8' => array(),
        'ie9' => array()
    );

    /**
     *
     * @var array $css contaneier styles files used in render template view 
     */
    private $css = array(
        'header' => array(),
        'footer' => array(),
        'ie8' => array(),
        'ie9' => array()
    );

    /**
     *
     * @var string $template set folder active template
     *  
     */
    public $template = 'default';

    /**
     *
     * @var mixed[] $data this container data for render in view 
     * 
     */
    public $data = array();

    /**
     *
     * @var boolean $init
     *  
     */
    private $init = false;

    /**
     *
     * @var mixed[] $gets container QUERY_STRING url  
     */
    var $gets = array();

    function __construct() {
        parent::__construct();
        //$this->online_users->OnlineUsers();
        if (isset($_SERVER ['QUERY_STRING'])):
            parse_str($_SERVER ['QUERY_STRING'], $this->gets);
        endif;
    }

    /**
     * initialize
     * 
     */
    private function initialize() {
        $dfl = config_item('template_' . $this->template);
        if ($dfl) {
            $this->js = array_merge($this->js, $dfl ['js']);
            $this->css = array_merge($this->css, $dfl ['css']);
        }
        $this->init = true;
    }

    /**
     * 
     * @param mixed[] $view 
     * 
     */
    public function template($view) {
        if (!$this->init) {
            $this->initialize();
        }
        $this->data ['site_content'] = '';
        $this->data ['side_bar_left'] = '';
        if (!is_array($view)) :
            $view = array(
                $view
            );

        endif;


        foreach ($view as $v) :
            $this->data ['site_content'] .= $this->load->view($v, $this->data, true);
        endforeach;
        $this->load->view('frontend/templates/' . $this->template . '.phtml', $this->data);
    }

    function add_js($file, $position = 'header', $priority = 10) {
        if (!$this->init) {
            $this->initialize();
        }

        if (isset($this->js [$position]) && !in_array($file, $this->js [$position])) {
            $this->js [$position] [] = array(
                'file' => $file,
                'priority' => $priority
            );
        }
    }

    function add_css($file, $position, $priority = 10) {
        if (!$this->init) {
            $this->initialize();
        }
        if (isset($this->css [$position]) && !in_array($file, $this->css [$position])) {
            $this->css [$position] [] = array(
                'file' => $file,
                'priority' => $priority
            );
        }
    }

    function get_footer_css() {
        if (count($this->css ['footer']) > 0) :

            subvalSort($this->css ['footer'], 'priority');

        endif;
        return;
    }

    function get_footer_js() {
        if (count($this->js ['footer']) > 0) :

            return subvalSort($this->js ['footer'], 'priority');

        endif;
        return false;
    }

    function get_header_css() {
        if (count($this->css ['header']) > 0) :

            return subvalSort($this->css ['header'], 'priority');

        endif;

        return;
    }

    function get_header_js() {
        if (count($this->js ['header']) > 0) :

            return subvalSort($this->js ['header'], 'priority');

        endif;
        return false;
    }

    function get_ie8_css() {
        if (count($this->css ['ie8']) > 0) :

            return subvalSort($this->css ['ie8'], 'priority');

        endif;

        return;
    }

    function get_ie8_js() {
        if (count($this->js ['ie8']) > 0) :

            return subvalSort($this->js ['ie8'], 'priority');

        endif;
        return false;
    }

    function get_ie9_css() {
        if (count($this->css ['ie9']) > 0) :

            return subvalSort($this->css ['ie8'], 'priority');

        endif;

        return;
    }

    function get_ie9_js() {
        if (count($this->js ['ie9']) > 0) :

            return subvalSort($this->js ['ie9'], 'priority');

        endif;
        return false;
    }

    function set_nocache_headers() {


        $this->output->set_header("HTTP/1.0 200 OK");
        $this->output->set_header("HTTP/1.1 200 OK");
        $this->output->set_header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
        $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
    }

    function response_json($data) {
        header('content-type:application/json;');
        echo json_encode($data);
        exit;
    }

}
